package com.lml.jdk8.test06;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * @author Administrator
 * @date 2020/2/29 14:59
 * Description:
 */
public class DemoTest02 {
    List<Student> list = Arrays.asList(
            new Student("lili", 19, 3160.8),
            new Student("lili", 19, 2160.8),
            new Student("feifei", 12, 3170.8),
            new Student("lulu", 20, 4450.8),
            new Student("nana", 17, 5500),
            new Student("xiaoming", 21, 6666.6)
    );

    @Test
    void fun11() {
        //求:所有员工的工资之和
        Double total = list.stream().map(Student::getSalary).
                reduce(0.0, Double::sum);
        System.out.println(total);
    }

    @Test
    void fun10() {
        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
        Integer result = list.stream()
                .reduce(0, Integer::sum);
        System.out.println(result);
        //结果: 15
    }


    @Test
    void fun9() {
        list.stream().forEach(System.out::println);
    }

    @Test
    void fun8() {
        Optional<Student> op = list.stream().
                min((x, y) -> x.getAge() - y.getAge());
        System.out.println(op.get());
    }

    @Test
    void fun7() {
        Optional<Student> op = list.stream().
                max((x, y) -> x.getAge() - y.getAge());
        System.out.println(op.get());
    }

    @Test
    void fun6() {
        long count = list.stream().count();
        System.out.println(count);
        //6
    }

    @Test
    void fun5() {
        Optional<Student> op = list.stream().
                filter(x -> x.getAge() == 21).
                findAny();
        System.out.println(op.get());
    }

    @Test
    void fun4() {
        Optional<Student> op = list.stream().
                sorted((x, y) -> x.getAge() - y.getAge()).
                findFirst();
        System.out.println(op.get());
    }

    @Test
    void fun3() {
        //判断 是否所有的人都没有18岁
        boolean b = list.stream().
                noneMatch(x -> x.getAge() >= 18);
        System.out.println(b);
        //false
    }

    @Test
    void fun2() {
        //判断: 是否有任何一人大于18岁
        boolean b = list.stream().
                anyMatch(x -> x.getAge() >= 18);
        System.out.println(b);
        //true
    }

    @Test
    void fun1() {
        //判断: 是否所有的人都大于18岁
        boolean b = list.stream().
                allMatch(x -> x.getAge() >= 18);
        System.out.println(b);
        //false
    }

}
